package com.surelution.projects.echo.domain

import grails.plugins.springsecurity.Secured;

@Secured(["ROLE_ADMIN"])
class PredefineMessageController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [predefineMessageInstanceList: PredefineMessage.list(params), predefineMessageInstanceTotal: PredefineMessage.count()]
    }

    def create = {
        def predefineMessageInstance = new PredefineMessage()
        predefineMessageInstance.properties = params
        return [predefineMessageInstance: predefineMessageInstance]
    }

    def save = {
        def predefineMessageInstance = new PredefineMessage(params)
        if (predefineMessageInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'predefineMessage.label', default: 'PredefineMessage'), predefineMessageInstance.id])}"
            redirect(action: "show", id: predefineMessageInstance.id)
        }
        else {
            render(view: "create", model: [predefineMessageInstance: predefineMessageInstance])
        }
    }

    def show = {
        def predefineMessageInstance = PredefineMessage.get(params.id)
        if (!predefineMessageInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'predefineMessage.label', default: 'PredefineMessage'), params.id])}"
            redirect(action: "list")
        }
        else {
            [predefineMessageInstance: predefineMessageInstance]
        }
    }

    def edit = {
        def predefineMessageInstance = PredefineMessage.get(params.id)
        if (!predefineMessageInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'predefineMessage.label', default: 'PredefineMessage'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [predefineMessageInstance: predefineMessageInstance]
        }
    }
	
	def editWithKey = {
		def key = params.code
		def predefineMessageInstance = PredefineMessage.findByCode(key)
		return [predefineMessageInstance: predefineMessageInstance]
	}

    def update = {
        def predefineMessageInstance = PredefineMessage.get(params.id)
        if (predefineMessageInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (predefineMessageInstance.version > version) {
                    
                    predefineMessageInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'predefineMessage.label', default: 'PredefineMessage')] as Object[], "Another user has updated this PredefineMessage while you were editing")
                    render(view: "edit", model: [predefineMessageInstance: predefineMessageInstance])
                    return
                }
            }
            predefineMessageInstance.properties = params
            if (!predefineMessageInstance.hasErrors() && predefineMessageInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'predefineMessage.label', default: 'PredefineMessage'), predefineMessageInstance.id])}"
                //redirect(action: "show", id: predefineMessageInstance.id)
//				redirect(action:"editWithKey", code: predefineMessageInstance.code)
				render(view: "editWithKey", model: [predefineMessageInstance: predefineMessageInstance])
            }
            else {
                render(view: "edit", model: [predefineMessageInstance: predefineMessageInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'predefineMessage.label', default: 'PredefineMessage'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def predefineMessageInstance = PredefineMessage.get(params.id)
        if (predefineMessageInstance) {
            try {
                predefineMessageInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'predefineMessage.label', default: 'PredefineMessage'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'predefineMessage.label', default: 'PredefineMessage'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'predefineMessage.label', default: 'PredefineMessage'), params.id])}"
            redirect(action: "list")
        }
    }
}
